package com.cbtx.tablet.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.cbtx.tablet.R

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/7 12:58 PM
 * desc   :
 */
class LastTimeConfirmDialog(private var mContext: Context) : Dialog(mContext, R.style.Dialog) {

    var onDialogListener: OnDialogListener? = null

    var handler = Handler()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCancelable(false)
        initView()
    }

    var laTime: TextView? = null

    private fun initView() {
        val contentView: View =
            LayoutInflater.from(mContext).inflate(R.layout.dialog_comfig_last_time, null)
        laTime = contentView.findViewById(R.id.tv_lastTime)
        val tvCancel = contentView.findViewById<TextView>(R.id.tv_cancel)
        val tvAgree = contentView.findViewById<TextView>(R.id.tv_agree)
        tvCancel.setOnClickListener {
            handler.removeCallbacksAndMessages(null)
            if (onDialogListener != null) {
                onDialogListener!!.onCancel()
                dismiss()
            }
        }
        tvAgree.setOnClickListener {
            handler.removeCallbacksAndMessages(null)
            if (onDialogListener != null) {
                onDialogListener!!.onAgree()
                dismiss()
            }
        }
        showTitme()
        setCancelable(true)
        setContentView(contentView)
    }


    var time = 30

    private fun showTitme() {
        handler.postDelayed({
            time -= 1
            laTime!!.text = time.toString() + ""
            if (time >= 1) {
                showTitme()
            } else {
                onDialogListener!!.onCancel()
                dismiss()
            }
        }, 1000)
    }

    fun setOnDialogListener(onDialogListener: OnDialogListener): LastTimeConfirmDialog? {
        this.onDialogListener = onDialogListener
        return this
    }


    interface OnDialogListener {
        fun onCancel()
        fun onAgree()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return keyCode == KeyEvent.KEYCODE_SEARCH
    }
}